package com.cj.service;

import com.cj.dao.CourseDao;
import com.cj.dao.CourseDetailDao;
import com.cj.entity.Course;
import com.cj.entity.Coursedetail;
import com.cj.entity.PageBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CourseService {
    CourseDao dao =  new CourseDao();
    CourseDetailDao courseDetailDao =new CourseDetailDao();
    public int insertCourse(Course c) {
        String sql = "insert into course values(null,?,?,?,?,?,?,?,?)";
        return dao.update(sql, c.getCourseName(), c.getDescs(), c.getCourseType(), c.getCourseImage()
                , c.getCourseVideo(), c.getCoursePrice(), c.getStatus(), new Date());

    }

    public PageBean<Course> findPage(String currentPage, String pageSize, String search) {
        int cPage = Integer.parseInt(currentPage);
        int size = Integer.parseInt(pageSize);

        PageBean<Course> pb = new PageBean<>();
        pb.setPageSize(size);
        pb.setCurrentPage(cPage);


//        总数是search 带条件查出来的参数，不是所有的count(*)
//        int totalCount = dao.getTotalCount("select count(*) from course");
//        pb.setTotalCount(totalCount);



////        获取数据
//        查询对象的sql
        StringBuilder sql = new StringBuilder("select * from course where 1=1 ");
//        查询对象条数的sql
        StringBuilder countSql = new StringBuilder("select count(*) from course where 1=1 ");
        //        存放参数
        ArrayList<Object> params = new ArrayList<>();
        System.out.println(search);
//        search不等于空，并且不等于空字符       不等和等！！！！
        if (search != null && !"".equals(search.trim())){
            sql.append("and courseName like ? ");
            countSql.append("and courseName like ?  ");
            params.add("%"+search+"%");
        }

        //        总数是根据search查出来的总数，   StringBuilder和ArrayList转换
        int totalCount = dao.getTotalCount(countSql.toString(),params.toArray());
        pb.setTotalCount(totalCount);

        int totalPage = totalCount%size==0? totalCount/size : totalCount/size+1;
        pb.setTotalPage(totalPage);


        sql.append("  limit ?,?");
        params.add((cPage-1)*size);
        params.add(size);
//        要将 StringBuilder 转为 String     ..一共三个参数，要将集合转为数组，才是三个参数
        List<Course> courseList = dao.getEntities(sql.toString(), Course.class, params.toArray());
        System.out.println(courseList);
        pb.setList(courseList);


        System.out.println(pb);
        return pb;
    }

    public int updateCourse(Course c) {
        String sql = "update course set courseName=?,descs=?,courseType=?,courseImage=?,courseVideo=?,coursePrice=?,status=?,createTime=? where cid = ? ";
        return dao.update(sql, c.getCourseName(), c.getDescs(), c.getCourseType(), c.getCourseImage()
                , c.getCourseVideo(), c.getCoursePrice(), c.getStatus(), new Date(),c.getCid());
    }


    public int deleteByCids(String ids) {
    String sql= "delete from course where cid in ("+ids+")";
      return  dao.update(sql);

    }

    public List<Course> findAllCourse(String uid) {
        String sql="select * from course where cid not in (select cid from course_user where uid=?)";
        return dao.getEntities(sql, Course.class,uid);

    }

    public List<Course> findCourseByType(String type) {
        String sql="";
        if("1".equals(type)){
            sql="select * from course where courseType=? limit 0,8";
        }else if("2".equals(type)){
            sql="select * from course where courseType=? limit 0,9";
        }else if("3".equals(type)){
            sql="select * from course where courseType=? limit 0,6";
        }
        return dao.getEntities(sql,Course.class,type);
    }

    public PageBean<Course> findPage(String currentPage, String pageSize, String search, String type) {

        PageBean<Course> pb = new PageBean<>();
        int cPage = Integer.parseInt(currentPage);
        int size = Integer.parseInt(pageSize);

        pb.setCurrentPage(cPage);
        pb.setPageSize(size);

        StringBuilder sql = new StringBuilder("select * from course where 1=1  ");
        ArrayList<Object> params = new ArrayList<>();
        if (search!=null && !"".equals(search.trim())){
            sql.append("  and courseName like ?  ");
            params.add("%"+search+"%");
        }

        if(type!=null && !"".equals(type.trim()) && !"0".equals(type.trim())){
            sql.append(" and courseType =?");
            params.add(type);
        }

        int totalCount = dao.getEntities(sql.toString(), Course.class, params.toArray()).size();
        int totalPage=totalCount%size==0? totalCount/size : totalCount/size+1;
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);

//        加上 limit
        sql.append(" limit ?,?");
        params.add((cPage-1)*size);
        params.add(size);

//        refresh 自己的服务器访问没有refresh
        List<Course> courseList = dao.getEntities(sql.toString(), Course.class, params.toArray());
        pb.setList(courseList);

        return pb;
    }

    public Course findCourse(String cid) throws ParseException {
        String sql ="select * from course where cid =?";
        Course course = dao.getEntity(sql, Course.class, cid);

        String time = course.getCreateTime();
        System.out.println(time);
   /*     String dataFormatStr= "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(dataFormatStr);
        String formatDate = sdf.format(createTime);*/
//        course.setCreateTime(formatDate);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int lastIndex = course.getCreateTime().indexOf(".");

//        System.out.println(course.getCreateTime().substring(0,lastIndex));
//        course.setCreateTime(course.getCreateTime().substring(0,lastIndex));

        // 将指定格式的字符串转换为 日期
        Date createTime = sdf.parse(course.getCreateTime());

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd ");
//        将日期变成指定格式的字符串
        String date = sdf1.format(createTime);
        course.setCreateTime(date);

        sql="select * from coursedetail where cid =? ";
        List<Coursedetail> coursedetailList = courseDetailDao.getEntities(sql, Coursedetail.class, cid);
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy.MM.dd HH：mm");
//        格式化start_data
        for (Coursedetail coursedetail : coursedetailList) {
//            System.out.println(coursedetail.getStart_data());
            String formatDate = sdf1.format(sdf3.parse(coursedetail.getStart_data()));
            coursedetail.setStart_data(formatDate);
        }
        course.setList(coursedetailList);
        return course;
    }

    public List<Course> findCourseByUser(String uid) {
        String sql = "select * from course where cid in (select cid from course_user where uid =?)";

        return dao.getEntities(sql, Course.class, uid);
    }
}
